package edu.whut.mall.admin.controller;


import edu.whut.mall.admin.domain.GroupBuyOrderSearchParams;
import edu.whut.mall.admin.service.IGroupBuyOrderService;
import edu.whut.mall.admin.vo.GroupBuyOrderDetailVo;
import edu.whut.mall.admin.vo.GroupBuyOrderVo;
import edu.whut.mall.common.api.CommonPage;
import edu.whut.mall.common.api.CommonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * @ClassName RestGroupBuyOrderController
 * @Description 团购订单控制器
 * @Author lvfanbing
 * @Date 2020-11-09 16:32
 * @Version 1.0
 */
@Slf4j
@RequestMapping("/groupBuyOrder")
@RequiredArgsConstructor
@RestController
@Api(value = "团购订单管理器", tags = {"团购订单管理器"})
public class RestGroupBuyOrderController {

    private final IGroupBuyOrderService groupBuyOrderService;

    @ApiOperation(value = "根据条件获取订单列表")
    @PostMapping("/listGroupBuyOrderByCondition")
    public CommonResult listGroupBuyOrderByCondition(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                     @RequestParam(value = "size",defaultValue = "10") Integer size,
                                                     @RequestBody GroupBuyOrderSearchParams groupBuyOrderSearchParams) {
        CommonPage<GroupBuyOrderVo> groupBuyOrderVoCommonPage = groupBuyOrderService.listGroupBuyOrderByCondition(groupBuyOrderSearchParams, pageNum, size);
        if (groupBuyOrderVoCommonPage != null) {
            return CommonResult.success(groupBuyOrderVoCommonPage, "获取订单列表成功");
        }
        return CommonResult.failed("获取订单列表失败");
    }

    @ApiOperation(value = "根据团购订单id获取订单详情")
    @GetMapping("/getGroupBuyOrderDetailByGroupBuyOrderId")
    public CommonResult getGroupBuyOrderDetailByGroupBuyOrderId(@RequestParam Long groupBuyOrderId) {
        GroupBuyOrderDetailVo groupBuyOrderDetailVo = groupBuyOrderService.getGroupBuyOrderDetailByGroupBuyOrderId(groupBuyOrderId);
        if (groupBuyOrderDetailVo != null) {
            return CommonResult.success(groupBuyOrderDetailVo, "根据订单id获取订单详情成功");
        }
        return CommonResult.failed("根据订单id获取订单详情失败");
    }

    @ApiOperation(value = "团购订单发货")
    @PostMapping("/groupBuyOrderConsign")
    public CommonResult groupBuyOrderConsign(@RequestParam Long orderId,
                                             @RequestParam Integer logisticsPlatform,
                                             @RequestParam String logisticsNumber) {
        int result = groupBuyOrderService.groupBuyOrderConsign(orderId, logisticsPlatform, logisticsNumber);
        if (result >= 0) {
            return CommonResult.success("团购订单发货成功");
        }
        return CommonResult.failed("团购订单发货失败");
    }

    @ApiOperation(value = "删除一个订单")
    @PostMapping("/deleteOneGroupBuyOrder")
    public CommonResult deleteOneGroupBuyOrder(@RequestParam Long orderId) {
        int result = groupBuyOrderService.deleteOneGroupBuyOrder(orderId);
        if (result >= 0) {
            return CommonResult.success("团购订单删除成功");
        }
        return CommonResult.failed("团购订单删除失败");
    }
}
